import javax.xml.bind.annotation.XmlID;

/**
 * @author admin
 * 69. x 的平方根
 *解法：二分查找算法，判断条件就是中间点的平方是否小于等于输入值
 */

public class LeeCode69 {

    public int mySqrt(int x) {

        if(x == 0) {
            return 0;
        }
        int left = 0;
        int right = x;
        int temp = -1;
        while ((long)left <= right){
            int mid = (left + right)/2;
            if(mid * mid <=x){
                temp = mid;
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return temp;
    }

    public static void main(String[] args) {
        System.out.println(new LeeCode69().mySqrt(4));
    }

}